package com.binfenhulian.chinaenergy.ces.utils.date;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by wanghjbuf on 2018/3/10.
 */
public class UnixTimestampUtils {

    /**
     * 获取当前unix时间戳
     * @return
     */
    public static int currentTimes() {
        return (int) (System.currentTimeMillis()/1000);
    }

    /**
     * 转化固定格式字符串为unix时间戳
     * @param formats
     * @param dateStr
     * @return
     * @throws Exception
     */
    public static int converStringToUnixTimes(String formats, String dateStr) throws Exception{
        if (formats.isEmpty()) {
            formats = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formats, Locale.CHINA);
        return (int) (simpleDateFormat.parse(dateStr).getTime()/1000);
    }

    /**
     * 转化固定格式时间
     * @param unixTimestamp
     * @param formats
     * @return
     */
    public static String covertUnixTimesToString(int unixTimestamp, String formats) {
        if (formats.isEmpty()) {
            formats = "yyyy-MM-dd HH:mm:ss";
        }
        Long timestamp = (long)unixTimestamp * 1000;
        return new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
    }

    /**
     * 转化时间
     * @param unixTimestamps
     * @return
     */
    public static Date covertUnixTimesToDate(int unixTimestamps) {
        return  new Date((long) unixTimestamps * 1000);
    }
}
